package pet_pavilion.yushouge.ams.controller;

import cn.tedu.yushouge.commons.consts.HttpConsts;
import cn.tedu.yushouge.commons.pojo.vo.PageData;
import cn.tedu.yushouge.commons.security.LoginPrincipal;
import cn.tedu.yushouge.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import pet_pavilion.yushouge.ams.pojo.dto.AdminLoginInfoDTO;
import pet_pavilion.yushouge.ams.pojo.param.AdminAddNewParam;
import pet_pavilion.yushouge.ams.pojo.param.AdminLoginInfoParam;
import pet_pavilion.yushouge.ams.pojo.param.AdminUpdateInfoParam;
import pet_pavilion.yushouge.ams.pojo.vo.AdminListVO;
import pet_pavilion.yushouge.ams.pojo.vo.AdminStandardVO;
import pet_pavilion.yushouge.ams.service.AdminService;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@Api(tags = "01. 管理员管理模块")
@RequestMapping("/admin/")
@Slf4j
@Validated
public class AdminController {
    @Autowired
    private AdminService adminService;

    @Autowired
    PasswordEncoder passwordEncoder;

    @ApiOperation("添加管理员")
    @PostMapping("add-new")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid
                                     AdminAddNewParam adminAddNewParam) {
        adminService.addNew(adminAddNewParam);
        return JsonResult.ok();
    }

    @PostMapping("login")
    @ApiOperation("管理员登录")
    @ApiOperationSupport(order = 10)
    public JsonResult<AdminLoginInfoDTO> login(AdminLoginInfoParam adminLoginInfoParam, @ApiIgnore HttpServletRequest request) {
        log.debug("开始处理【管理员登录】的请求，参数：{}", adminLoginInfoParam);
        String remoteAddr = request.getRemoteAddr();
        String userAgent = request.getHeader(HttpConsts.HEADER_USER_AGENT);
        AdminLoginInfoDTO loginInfo = adminService.login(adminLoginInfoParam,remoteAddr,userAgent);
        log.debug("即将响应：{}", loginInfo);
        return JsonResult.ok(loginInfo);
    }


    @PostMapping("/logout")
    @ApiOperation("管理员退出登录")
    @ApiOperationSupport(order = 11)
    public JsonResult<Void> logout(@RequestHeader(HttpConsts.HEADER_AUTHORIZATION) String jwt) {
        log.debug("开始处理【管理员退出登录】的请求，参数：{}", jwt);
        adminService.logout(jwt);
        return JsonResult.ok();
    }


    @PostMapping("delete")
    @ApiOperation("根据ID删除管理员")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "管理员ID", required = true, dataType = "long")
    })
    public JsonResult delete(@Range(min = 1, message = "根据ID删除管理员失败，请提交合法的ID值！")
                             @RequestParam Long id,@ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal) {
        log.debug("开始处理【根据ID删除管理员】的请求，参数：{}", id);
        log.debug("当事人ID：{}", loginPrincipal.getId());
        log.debug("当事人用户名：{}", loginPrincipal.getUsername());
        adminService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("update")
    @ApiOperation("修改管理员详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "管理员ID", required = true, dataType = "long")
    })
    public JsonResult updateInfoById(@RequestParam @Range(min = 1, message = "请提交有效的管理员ID值！") Long id,
                                     @Valid AdminUpdateInfoParam adminUpdateInfoParam,
                                     @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal) {
        log.debug("开始处理【修改管理员详情】的业务，参数：{}", adminUpdateInfoParam);
        log.debug("当事人ID：{}", loginPrincipal.getId());
        log.debug("当事人用户名：{}", loginPrincipal.getUsername());
        adminUpdateInfoParam.setPassword(passwordEncoder.encode(adminUpdateInfoParam.getPassword()));
        adminService.updateById(id, adminUpdateInfoParam);
        return JsonResult.ok();
    }

    @PostMapping("enable")
    @ApiOperation("启用管理员")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "管理员ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setEnable(@Range(min = 1, message = "请提交有效的管理员ID值！") @RequestParam Long id) {
        log.debug("开始处理【启用管理员】的请求，参数：{}", id);
        adminService.setEnable(id);
        return JsonResult.ok();
    }

    @PostMapping("disable")
    @ApiOperation("禁用管理员")
    @ApiOperationSupport(order = 320)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "管理员ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setDisable(@Range(min = 1, message = "请提交有效的管理员ID值！") @RequestParam Long id) {
        log.debug("开始处理【禁用管理员】的请求，参数：{}", id);
        adminService.setDisable(id);
        return JsonResult.ok();
    }

    @GetMapping("standard")
    @ApiOperation("根据ID查询管理员详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "管理员ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(@Range(min = 1, message = "根据ID查询管理员详情失败，请提交合法的ID值！")
                                      @RequestParam Long id,
                                      @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal) {
        log.debug("开始处理【根据ID查询管理员详情】的请求，参数：{}", id);
        log.debug("当事人ID：{}", loginPrincipal.getId());
        log.debug("当事人用户名：{}", loginPrincipal.getUsername());
        AdminStandardVO result = adminService.getStandardById(id);
        return JsonResult.ok(result);
    }

    @GetMapping("list")
    @ApiOperation("查询管理员列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query")
    })
    public JsonResult list(@Range(min = 1, message = "查询相册列表失败，请提供正确的页码值！") Integer page,
                           @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal){
        log.debug("开始处理【根据页码查询管理员列表】的请求，参数：{}", page);
        log.debug("当事人ID：{}", loginPrincipal.getId());
        log.debug("当事人用户名：{}", loginPrincipal.getUsername());
        if (page == null || page < 1) {
            page = 1;
        }
        PageData<AdminListVO> pageData = adminService.list(page);
        return JsonResult.ok(pageData);
    }
}
